/*
Borealis Library
A library of commonly used hydrological modeling building blocks

Copyright (C) 2008  Chris Marsh

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


#include "include\borealis\Clock.hpp"

namespace Borealis
{


	void Clock::tic()
	{

		#ifdef _WIN32
				LARGE_INTEGER startCount;  
				startCount.QuadPart = 0;
				QueryPerformanceCounter(&startCount);
				m_timers.push(startCount);
		#else
				timeval startCount;  
				gettimeofday(&startCount, NULL);
		#endif
	}

	Clock::Clock()
	{
		
		#ifdef _WIN32
				QueryPerformanceFrequency(&m_frequency);
		#endif

	}

	double Clock::toc()
	{
		double m_start;               
		double m_end;    

		#ifdef _WIN32
				LARGE_INTEGER endCount;
				endCount.QuadPart = 0;

				QueryPerformanceCounter(&endCount);

				if(m_timers.empty())
					return -1.0;

				m_start = (m_timers.top()).QuadPart * (1000000.0 / m_frequency.QuadPart);
				m_timers.pop();
				m_end = endCount.QuadPart * (1000000.0 / m_frequency.QuadPart);
		#else
				timeval endCount; 
				gettimeofday(&endCount, NULL);

				m_start = ((m_timers.top()).tv_sec * 1000000.0) + (m_timers.top()).tv_usec;
				m_timers.pop();
				m_end = (endCount.tv_sec * 1000000.0) + endCount.tv_usec;
		#endif

		return m_end - m_start;
	}
}